<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>sms 升级部署 </title>
    <link href="//cdn.staticfile.org/layui/2.8.2/css/layui.css" rel="stylesheet">
    <style>
        .logDiv {
            width: 100%;
            height: 400px;
            overflow: auto;
        }

        .dirDiv {
            width: 100%;
            height: 200px;
            overflow: auto;
        }

        .log-line {
            font-size: 15px;
            padding: 5px;
        }
    </style>
</head>

<body>
    <!-- HTML Content -->
    <ul class="layui-nav" lay-filter="demo-filter-nav">
        <li class="layui-nav-item layui-this">
            <a href="/upgradeCode">升级代码</a>
        </li>
        <li class="layui-nav-item">
            <a href="/appConf">配置文件</a>
        </li>
        <li class="layui-nav-item">
            <a href="/script">监控脚本</a>
        </li>
        <li class="layui-nav-item">
            <a href="/nginxConf">nginx配置</a>
        </li>
        <li class="layui-nav-item">
            <a href="/allLog">全部日志</a>
        </li>
        <li class="layui-nav-item">
            <a href="/errorLog">错误日志</a>
        </li>
    </ul>

    <!--升级模块-->
    <div class="layui-bg-gray module" style="padding: 16px;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        Web:
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-radius" lay-active="w1">
                            升级代码
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"
                            lay-active="w2">
                            删除升级文件
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm layui-btn-radius"
                            lay-active="w3">
                            删除源码文件
                        </button>
                    </div>
                    <div class="layui-card-body">
                        <p>实时日志</p>
                        <div class="logDiv">
                            <pre class="layui-code code-demo logDiv" id="webDiv"></pre>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        大屏:
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-radius" lay-active="s1">
                            升级代码
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"
                            lay-active="s2">
                            删除升级文件
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm layui-btn-radius"
                            lay-active="s3">
                            删除源码文件
                        </button>
                    </div>

                    <div class="layui-card-body">
                        <p>实时日志</p>
                        <div class="logDiv">
                            <pre class="layui-code code-demo logDiv" id="screenDiv"></pre>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        OpenApi:
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-radius" lay-active="o1">
                            升级代码
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal layui-btn-radius"
                            lay-active="o4">
                            重启服务
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"
                            lay-active="o2">
                            删除升级文件
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm layui-btn-radius"
                            lay-active="o3">
                            删除源码文件
                        </button>
                    </div>
                    <div class="layui-card-body">
                        <p>实时日志</p>
                        <div class="logDiv">
                            <pre class="layui-code code-demo logDiv" id="OpenApiDiv"></pre>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">
                        Java:
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-radius" lay-active="j1">
                            升级代码
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-normal layui-btn-radius"
                            lay-active="j4">
                            重启服务
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"
                            lay-active="j2">
                            删除升级文件
                        </button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm layui-btn-radius"
                            lay-active="j3">
                            删除源码文件
                        </button>
                    </div>
                    <div class="layui-card-body">
                        <p>实时日志</p>
                        <div class="logDiv">
                            <pre class="layui-code code-demo logDiv" id="javaDiv"></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button type="button" class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius"
                        lay-active="d1">
                        查看升级目录
                    </button>
                    <button type="button" class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius"
                        lay-active="d2">
                        查看前端源码目录
                    </button>
                    <button type="button" class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius"
                        lay-active="d3">
                        查看后端源码目录
                    </button>
                    <button type="button" class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius"
                        lay-active="d4">
                        查看Java程序进程
                    </button>
                    <button type="button" class="layui-btn layui-btn-xs layui-btn-primary layui-btn-radius"
                        lay-active="d5">
                        查看端口占用
                    </button>
                </div>
                <div class="layui-card-body">
                    <div class="dirDiv">
                        <pre class="layui-code code-demo logDiv" id="dirDiv"></pre>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <script src="//cdn.staticfile.org/layui/2.8.2/layui.js"></script>
    <script>
        layui.use(['jquery'], function () {
            // 引用 jquery
            var $ = layui.$;

            var element = layui.element;
            var layer = layui.layer;

            // 导航点击事件
            element.on('nav(demo-filter-nav)', function (elem) {
                console.log(elem); // 得到当前点击的元素 jQuery 对象
            });

            // 升级代码模块
            var util = layui.util;
            var wsw;
            var wss;
            var wso;
            var wsj;

            var ip = window.location.host.split(":")[0];

            util.event('lay-active', {
                w1: function () {
                    layer.confirm('确定要升级 Web 服务么？', {
                        btn: ['确定', '关闭']
                    }, function () {
                        layer.msg('正在进行升级', { icon: 1 });
                        if (!window.WebSocket) {
                            if (window.MozWebSocket) {
                                window.WebSocket = window.MozWebSocket;
                            } else {
                                $('#webDiv').prepend("<p>你的浏览器不支持websocket</p>");
                            }
                        }
                        if (wsw && wsw.readyState == 1) {
                            layer.msg("websocket链接已经是打开状态了, 请一分钟之后再次重试。");
                        } else {
                            $("#webDiv").html("");
                            wsw = new WebSocket('ws://' + ip + ':18080/ws/upgrade?appCode=web');
                            wsw.onopen = function (evt) {
                                $('#webDiv').append("<div class='log-line'>升级web的websocket链接成功, 链接将于60s后自动关闭.</div>");
                            }
                            wsw.onclose = function (evt) {
                                $('#webDiv').append("<div class='log-line'>升级web的websocket成功关闭</div>");
                            }
                            wsw.onmessage = function (evt) {
                                let item = $("<div class='log-line'></div>");
                                item.text(evt.data);
                                $("#webDiv").append(item);
                            }
                            // 一分钟后 自动关闭 执行结束事件
                            window.setTimeout(
                                function () {
                                    if (wsw && wsw.readyState == 1) {
                                        layer.msg("将关闭升级web的websocket链接.");
                                        wsw.close();
                                    } else {
                                        layer.msg("升级web的websocket链接已经是关闭状态.");
                                    }
                                }
                                , 1000 * 60);
                        }
                    }, function () {
                        layer.msg('取消升级 Web 服务', { icon: 0 });
                    });
                },
                w2: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/root/vcom/ddc_update/rm_sms_tgz.sh"
                        , function (data, status) {
                            layer.msg("Web历史升级文件已删除成功.");
                        }
                    );
                },
                w3: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/usr/local/nginx/html/rm_sms_dir.sh"
                        , function (data, status) {
                            layer.msg("Web历史源码文件已删除成功.");
                        }
                    );
                },
                s1: function () {
                    layer.confirm('确定要升级 大屏 服务么？', {
                        btn: ['确定', '关闭']
                    }, function () {
                        layer.msg('正在进行升级', { icon: 1 });
                        if (!window.WebSocket) {
                            if (window.MozWebSocket) {
                                window.WebSocket = window.MozWebSocket;
                            } else {
                                $('#webDiv').prepend("<p>你的浏览器不支持websocket</p>");
                            }
                        }
                        if (wss && wss.readyState == 1) {
                            layer.msg("websocket链接已经是打开状态了, 请一分钟之后再次重试。");
                        } else {
                            $("#screenDiv").html("");
                            wss = new WebSocket('ws://' + ip + ':18080/ws/upgrade?appCode=screen');
                            wss.onopen = function (evt) {
                                $('#screenDiv').append("<div class='log-line'>升级大屏的websocket链接成功, 链接将于60s后自动关闭.</div>");
                            }
                            wss.onclose = function (evt) {
                                $('#screenDiv').append("<div class='log-line'>升级大屏的websocket成功关闭</div>");
                            }
                            wss.onmessage = function (evt) {
                                let item = $("<div class='log-line'></div>");
                                item.text(evt.data);
                                $("#screenDiv").append(item);
                            }
                            // 一分钟后 自动关闭 执行结束事件
                            window.setTimeout(
                                function () {
                                    if (wss && wss.readyState == 1) {
                                        layer.msg("将关闭升级大屏的websocket链接.");
                                        wss.close();
                                    } else {
                                        layer.msg("升级大屏的websocket链接已经是关闭状态.");
                                    }
                                }
                                , 1000 * 60);
                        }
                    }, function () {
                        layer.msg('取消升级 大屏 服务', { icon: 0 });
                    });

                },
                s2: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/root/vcom/ddc_update/rm_screen_tgz.sh"
                        , function (data, status) {
                            layer.msg("大屏历史升级文件已删除成功.");
                        }
                    );
                },
                s3: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/usr/local/nginx/html/rm_screen_dir.sh"
                        , function (data, status) {
                            layer.msg("大屏历史源码文件已删除成功.");
                        }
                    );
                },
                o1: function () {
                    layer.confirm('确定要升级 OpenApi 服务么？', {
                        btn: ['确定', '关闭']
                    }, function () {
                        layer.msg('正在进行升级', { icon: 1 });
                        if (!window.WebSocket) {
                            if (window.MozWebSocket) {
                                window.WebSocket = window.MozWebSocket;
                            } else {
                                $('#OpenApiDiv').prepend("<p>你的浏览器不支持websocket</p>");
                            }
                        }
                        if (wso && wso.readyState == 1) {
                            layer.msg("websocket链接已经是打开状态了, 请一分钟之后再次重试。");
                        } else {
                            $("#OpenApiDiv").html("");
                            wso = new WebSocket('ws://' + ip + ':18080/ws/upgrade?appCode=openapi');
                            wso.onopen = function (evt) {
                                $('#OpenApiDiv').append("<div class='log-line'>升级OpenApi的websocket链接成功, 链接将于60s后自动关闭.</div>");
                            }
                            wso.onclose = function (evt) {
                                $('#OpenApiDiv').append("<div class='log-line'>升级OpenApi的websocket成功关闭</div>");
                            }
                            wso.onmessage = function (evt) {
                                let item = $("<div class='log-line'></div>");
                                item.text(evt.data);
                                $("#OpenApiDiv").append(item);
                            }
                            // 一分钟后 自动关闭 执行结束事件
                            window.setTimeout(
                                function () {
                                    if (wso && wso.readyState == 1) {
                                        layer.msg("将关闭升级OpenApi的websocket链接.");
                                        wso.close();
                                    } else {
                                        layer.msg("升级OpenApi的websocket链接已经是关闭状态.");
                                    }
                                }
                                , 1000 * 60);
                        }
                    }, function () {
                        layer.msg('取消升级 OpenApi 服务', { icon: 0 });
                    });
                },
                o2: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/root/vcom/ddc_update/rm_openapi_tgz.sh"
                        , function (data, status) {
                            layer.msg("OpenApi历史升级文件已删除成功.");
                        }
                    );
                },
                o3: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/opt/gongxiangcity/rm_openapi_dir.sh"
                        , function (data, status) {
                            layer.msg("OpenApi历史源码文件已删除成功.");
                        }
                    );
                },
                o4: function () {
                    layer.confirm('确定要重启 OpenApi 服务么？', {
                        btn: ['确定', '关闭']
                    }, function () {
                        layer.msg('正在进行重启', { icon: 1 });
                        $("#OpenApiDiv").html("");
                        $.get(
                            "/cmd/sh?shellFilePath=/opt/gongxiangcity/restart_openapi.sh"
                            , function (data, status) {
                                layer.msg("重启OpenApi服务成功.");
                            }
                        );
                    }, function () {
                        layer.msg('取消重启 OpenApi 服务', { icon: 0 });
                    });
                },
                j1: function () {
                    layer.confirm('确定要升级 Java 服务么？', {
                        btn: ['确定', '关闭']
                    }, function () {
                        layer.msg('正在进行升级', { icon: 1 });
                        if (!window.WebSocket) {
                            if (window.MozWebSocket) {
                                window.WebSocket = window.MozWebSocket;
                            } else {
                                $('#javaDiv').prepend("<p>你的浏览器不支持websocket</p>");
                            }
                        }
                        if (wsj && wsj.readyState == 1) {
                            layer.msg("websocket链接已经是打开状态了, 请一分钟之后再次重试。");
                        } else {
                            $("#javaDiv").html("");
                            wsj = new WebSocket('ws://' + ip + ':18080/ws/upgrade?appCode=java');
                            wsj.onopen = function (evt) {
                                $('#javaDiv').append("<div class='log-line'>升级Java的websocket链接成功, 链接将于60s后自动关闭.</div>");
                            }
                            wsj.onclose = function (evt) {
                                $('#javaDiv').append("<div class='log-line'>升级Java的websocket成功关闭</div>");
                            }
                            wsj.onmessage = function (evt) {
                                let item = $("<div class='log-line'></div>");
                                item.text(evt.data);
                                $("#javaDiv").append(item);
                            }
                            // 一分钟后 自动关闭 执行结束事件
                            window.setTimeout(
                                function () {
                                    if (wsj && wsj.readyState == 1) {
                                        layer.msg("将关闭升级Java的websocket链接.");
                                        wsj.close();
                                    } else {
                                        layer.msg("升级Java的websocket链接已经是关闭状态.");
                                    }
                                }
                                , 1000 * 60);
                        }
                    }, function () {
                        layer.msg('取消升级 Java 服务', { icon: 0 });
                    });
                },
                j2: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/root/vcom/ddc_update/rm_java_tgz.sh"
                        , function (data, status) {
                            layer.msg("Java历史升级文件已删除成功.");
                        }
                    );
                },
                j3: function () {
                    $.get(
                        "/cmd/sh?shellFilePath=/opt/gongxiangcity/rm_java_dir.sh"
                        , function (data, status) {
                            layer.msg("Java历史源码文件已删除成功.");
                        }
                    );
                },
                j4: function () {
                    layer.confirm('确定要重启 Java 服务么？', {
                        btn: ['确定', '关闭']
                    }, function () {
                        layer.msg('正在进行重启', { icon: 1 });
                        $("#javaDiv").html("");
                        $.get(
                            "/cmd/sh?shellFilePath=/opt/gongxiangcity/restart_java.sh"
                            , function (data, status) {
                                layer.msg("重启Java服务成功.");
                            }
                        );
                    }, function () {
                        layer.msg('取消重启 Java 服务', { icon: 0 });
                    });
                },
                d1: function () {
                    $("#dirDiv").html("");
                    $.get(
                        "/cmd/ll?dir=/root/vcom/ddc_update"
                        , function (data, status) {
                            let item = $("<div class='log-line'></div>");
                            item.text(data);
                            $("#dirDiv").append(item);
                        }
                    );
                },
                d2: function () {
                    $("#dirDiv").html("");
                    $.get(
                        "/cmd/ll?dir=/usr/local/nginx/html"
                        , function (data, status) {
                            let item = $("<div class='log-line'></div>");
                            item.text(data);
                            $("#dirDiv").append(item);
                        }
                    );
                },
                d3: function () {
                    $("#dirDiv").html("");
                    $.get(
                        "/cmd/ll?dir=/opt/gongxiangcity"
                        , function (data, status) {
                            let item = $("<div class='log-line'></div>");
                            item.text(data);
                            $("#dirDiv").append(item);
                        }
                    );
                },
                d4: function () {
                    $("#dirDiv").html("");
                    $.get(
                        "/cmd/ps?keyWords=java"
                        , function (data, status) {
                            let item = $("<div class='log-line'></div>");
                            item.text(data);
                            $("#dirDiv").append(item);
                        }
                    );
                },
                d5: function () {
                    $("#dirDiv").html("");
                    $.get(
                        "/cmd/cmd?cmd=netstat -ntlp"
                        , function (data, status) {
                            let item = $("<div class='log-line'></div>");
                            item.text(data);
                            $("#dirDiv").append(item);
                        }
                    );
                },
            });
        });

    </script>
</body>

</html>